

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.services.autowait &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>cup.services.autowait</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.services.autowait</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Qiang Liu, Guannan Ma</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    auto wait related modules.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;wait_until_file_exist&#39;</span><span class="p">,</span> <span class="s1">&#39;wait_until_reg_str_exist&#39;</span><span class="p">,</span>
    <span class="s2">&quot;wait_until_process_not_exist&quot;</span><span class="p">,</span> <span class="s2">&quot;wait_until_port_used&quot;</span><span class="p">,</span>
    <span class="s2">&quot;wait_until_process_used_ports&quot;</span><span class="p">,</span> <span class="s2">&quot;wait_until_port_not_used&quot;</span><span class="p">,</span>
    <span class="s2">&quot;wait_until_process_exist&quot;</span><span class="p">,</span> <span class="s2">&quot;wait_until_process_killed&quot;</span>
<span class="p">]</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup.shell</span> <span class="k">import</span> <span class="n">oper</span>


<div class="viewcode-block" id="wait_until_file_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_file_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_file_exist</span><span class="p">(</span>
    <span class="n">dst_path</span><span class="p">,</span> <span class="n">file_name</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">False</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait util the file exists or the function timeout</span>

<span class="sd">    :param dst_path:</span>
<span class="sd">        searching path</span>
<span class="sd">    :param file_name:</span>
<span class="sd">        filename, support *</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        max wating time until timeout</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        check interval</span>
<span class="sd">    :param recursive:</span>
<span class="sd">        recursively search or not</span>
<span class="sd">    :return:</span>
<span class="sd">        True if found.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">contains_file</span><span class="p">(</span><span class="n">dst_path</span><span class="p">,</span> <span class="n">file_name</span><span class="p">,</span> <span class="n">recursive</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_reg_str_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_reg_str_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_reg_str_exist</span><span class="p">(</span>
    <span class="n">dst_file_path</span><span class="p">,</span> <span class="n">reg_str</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until any line in the file matches the \</span>
<span class="sd">    reg_str(regular expression string)</span>

<span class="sd">    :param dst_file_path:</span>
<span class="sd">        searching path</span>
<span class="sd">    :param reg_str:</span>
<span class="sd">        regular expression string</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        maximum waiting time until timeout</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval</span>
<span class="sd">    :return:</span>
<span class="sd">        True if found</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">file_reader</span> <span class="o">=</span> <span class="n">FileReader</span><span class="p">(</span><span class="n">dst_file_path</span><span class="p">)</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">__check_reg_str_contain</span><span class="p">(</span><span class="n">file_reader</span><span class="p">,</span> <span class="n">reg_str</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_not_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_not_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_not_exist</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the process does not exist anymore or the function timeouts</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        process cwd</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        maximum waiting time until timeout. 10 seconds by default</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval, 0.5 second by default</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if the process disapper before timeout</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">process_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_proc_exist</span><span class="p">(</span><span class="n">pro_path</span><span class="p">,</span> <span class="n">pro_name</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_exist"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_exist">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_exist</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the process exists</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        the specific process working path</span>
<span class="sd">    :param max_wait_sec:</span>
<span class="sd">        maximum waiting time until timeout</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if the process is found before timeout</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">process_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">pro_name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">process_path</span><span class="p">)</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_proc_exist</span><span class="p">(</span><span class="n">pro_path</span><span class="p">,</span> <span class="n">pro_name</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_port_used"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_port_used">[docs]</a><span class="k">def</span> <span class="nf">wait_until_port_used</span><span class="p">(</span>
    <span class="n">port</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the port is used.  *Notice this function will invoke\</span>
<span class="sd">    a bash shell to execute command [netstat]!*</span>

<span class="sd">    :return:</span>
<span class="sd">        return True if the port is used</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_port_used</span><span class="p">(</span><span class="n">port</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_port_not_used"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_port_not_used">[docs]</a><span class="k">def</span> <span class="nf">wait_until_port_not_used</span><span class="p">(</span>
    <span class="n">port</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the port is free</span>

<span class="sd">    :return:</span>
<span class="sd">        return True if the port is free before timeout</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_port_used</span><span class="p">(</span><span class="n">port</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_used_ports"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_used_ports">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_used_ports</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">ports</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the process has taken the ports before timeouts</span>

<span class="sd">    :return:</span>
<span class="sd">        True if all ports are used by the specific process.</span>
<span class="sd">        False, otherwise</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="n">used_port_num</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">curr_port</span> <span class="ow">in</span> <span class="n">ports</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_process_used_port</span><span class="p">(</span><span class="n">process_path</span><span class="p">,</span> <span class="n">curr_port</span><span class="p">):</span>
                <span class="n">used_port_num</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">if</span> <span class="n">used_port_num</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ports</span><span class="p">):</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="wait_until_process_killed"><a class="viewcode-back" href="../../../cup.services.html#cup.services.autowait.wait_until_process_killed">[docs]</a><span class="k">def</span> <span class="nf">wait_until_process_killed</span><span class="p">(</span>
    <span class="n">process_path</span><span class="p">,</span> <span class="n">ports</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until the [process] does not exists and all [ports] are free</span>

<span class="sd">    :param process_path:</span>
<span class="sd">        process cwd</span>
<span class="sd">    :param ports:</span>
<span class="sd">        port list</span>
<span class="sd">    :param interval_sec:</span>
<span class="sd">        state check interval</span>
<span class="sd">    :return:</span>
<span class="sd">        True if all conditions meet.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="c1"># check process</span>
        <span class="k">if</span> <span class="kc">False</span> <span class="o">==</span> <span class="n">wait_until_process_not_exist</span><span class="p">(</span>
            <span class="n">process_path</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span>
        <span class="p">):</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
            <span class="k">continue</span>
        <span class="c1"># check ports</span>
        <span class="n">not_used_port_num</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">curr_port</span> <span class="ow">in</span> <span class="n">ports</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">oper</span><span class="o">.</span><span class="n">is_process_used_port</span><span class="p">(</span><span class="n">process_path</span><span class="p">,</span> <span class="n">curr_port</span><span class="p">):</span>
                <span class="n">not_used_port_num</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>
        <span class="k">if</span> <span class="n">not_used_port_num</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ports</span><span class="p">):</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span></div>


<span class="k">def</span> <span class="nf">_wait_until_return</span><span class="p">(</span><span class="n">func</span><span class="p">,</span>
        <span class="n">boolean</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
    <span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until function return [boolean]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">curr_wait_sec</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">curr_wait_sec</span> <span class="o">&lt;</span> <span class="n">max_wait_sec</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">==</span> <span class="n">boolean</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">interval_sec</span><span class="p">)</span>
            <span class="n">curr_wait_sec</span> <span class="o">+=</span> <span class="n">interval_sec</span>
    <span class="k">return</span> <span class="kc">False</span>


<span class="k">def</span> <span class="nf">wait_return_true</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until func return true or max_wait_sec passes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_wait_until_return</span><span class="p">(</span>
        <span class="n">func</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
    <span class="p">)</span>


<span class="k">def</span> <span class="nf">wait_return_false</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    wait until func return False or max_wait_sec passes.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">_wait_until_return</span><span class="p">(</span>
        <span class="n">func</span><span class="p">,</span> <span class="kc">False</span><span class="p">,</span> <span class="n">max_wait_sec</span><span class="p">,</span> <span class="n">interval_sec</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span>
    <span class="p">)</span>


<span class="k">def</span> <span class="nf">__check_reg_str_contain</span><span class="p">(</span><span class="n">file_reader</span><span class="p">,</span> <span class="n">reg_str</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    check if any line matches the reg_str</span>

<span class="sd">    :param file_reader:</span>
<span class="sd">        FileReade Object</span>
<span class="sd">    :return:</span>
<span class="sd">        return True if found</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">file_content</span> <span class="o">=</span> <span class="n">file_reader</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">lines</span> <span class="o">=</span> <span class="n">file_content</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">reg_str</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span>


<span class="k">class</span> <span class="nc">FileReader</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    this class is used to read file incremental</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">file_path</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file_path</span> <span class="o">=</span> <span class="n">file_path</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_read_size</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        read from last position</span>

<span class="sd">        :param max_read_size:</span>
<span class="sd">            maximum reading length</span>
<span class="sd">        :return:</span>
<span class="sd">            content read</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># whether the file exist</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_path</span><span class="p">):</span>
            <span class="k">return</span> <span class="s2">&quot;&quot;</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_path</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
            <span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">SEEK_END</span><span class="p">)</span>
            <span class="n">size</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">tell</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">:</span>
                <span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">SEEK_SET</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="o">&gt;</span> <span class="p">(</span><span class="n">size</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">)):</span>
                    <span class="n">max_read_size</span> <span class="o">=</span> <span class="n">size</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">max_read_size</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>  <span class="c1"># may be a new file with the same name</span>
                <span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">SEEK_SET</span><span class="p">)</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">max_read_size</span> <span class="o">&gt;</span> <span class="n">size</span><span class="p">):</span>
                    <span class="n">max_read_size</span> <span class="o">=</span> <span class="n">size</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">max_read_size</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">ret</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ret</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>